package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.activities.StudentTimeTableTab.StateOnItemSelectedListener;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;
import com.apiit.webserviceclient.utility.GlobalData;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class ApplyLeave extends Activity implements OnClickListener {
	
	Spinner leavetypeSpinner;
	Spinner workflowSpinner;
	Spinner personSpinner;
	Spinner leaveforSpinner;
	EditText reasontxt;
	DatePicker startdatePicker;
	Button apply;
	String selectedleaeType, selectedworkflow, selectedPerson, selectedLeavefor;
	int day,month,year;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_apply_leave);
		
		leavetypeSpinner = (Spinner) findViewById(R.id.leavetypeSpinner);
		workflowSpinner = (Spinner) findViewById(R.id.workflowSpinner);
		personSpinner = (Spinner) findViewById(R.id.personSpinner);
		leaveforSpinner = (Spinner) findViewById(R.id.leaveforSpinner);
		reasontxt = (EditText) findViewById(R.id.reasontxt);
		startdatePicker = (DatePicker) findViewById(R.id.startdatePicker);
		
		apply = (Button) findViewById(R.id.btnapplyleave);
		apply.setOnClickListener(this);
		
		leaveforSpinner.setOnItemSelectedListener(new StateOnItemSelectedListener());
		workflowSpinner.setOnItemSelectedListener(new StateOnItemSelectedListener());
		leaveforSpinner.setOnItemSelectedListener(new StateOnItemSelectedListener());
		personSpinner.setOnItemSelectedListener(new StateOnItemSelectedListener());
		
	}
	
	public class StateOnItemSelectedListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View itemSelected,
				int pos, long id) {
			selectedleaeType = leavetypeSpinner.getItemAtPosition(pos).toString();
			selectedworkflow = workflowSpinner.getItemAtPosition(pos).toString();
			selectedPerson = personSpinner.getItemAtPosition(pos).toString();
			selectedLeavefor = leaveforSpinner.getItemAtPosition(pos).toString();
			
			Log.d("charini", selectedleaeType);
			Log.d("charini", selectedworkflow);
			Log.d("charini", selectedPerson);
			Log.d("charini", selectedLeavefor);

		}

		public void onNothingSelected(AdapterView<?> parent) { // Do nothing
																// here
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.apply_leave, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	    day = startdatePicker.getDayOfMonth();
	    month = startdatePicker.getMonth() + 1;
	    year = startdatePicker.getYear();
	   
	    Log.d("DAY", "day" +day);
	    Log.d("month", "month" +month);
	    Log.d("year", "year" +year);
	    

		BPEL_Process bpelProcess = new BPEL_Process();
		BPEL_Request request = new BPEL_Request();
		request.setUrl("http://webspace1.apiit.lk/eHRM-Web/Leave/OnLineLeaveApplicationUI.aspx?FunctionId=531");

		Map<String, String> xpathMap = new HashMap<String, String>();
		xpathMap.put("text", "");
		request.setXpathMap(xpathMap);
		
		Map<String, String> parametersMap = new HashMap<String, String>();
		parametersMap.put("", "");
		parametersMap.put("", "");
		parametersMap.put("", "");
		parametersMap.put("", "");
		parametersMap.put("", "");
		parametersMap.put("","" );
		parametersMap.put("", "");
		
		request.setParametersMap(parametersMap);
		request.setType("");
		BPEL_Response response = null;
		try {
			response = bpelProcess.doRequest(request);

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			

		if (response.getStatus().equalsIgnoreCase(GlobalData.message_success)) {
			String testResult1 = response.getResultsMap().get("text");

			Toast.makeText(getApplicationContext(), testResult1,
					Toast.LENGTH_LONG).show();
			Intent i = new Intent(this, LecturerIndex.class);
			i.putExtra("text", testResult1);
			startActivity(i);

		}
	}
	
	

}
